package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.dao.MvTypeDao;
import com.entity.Mv;
import com.entity.Data.PagerData;
import com.entity.Query.MvQuery;
import com.service.MvService;
import com.service.MvTypeService;
import com.service.SingerService;

@RequestMapping("/mv")
@Controller
public class MvController {
	
	@Autowired
	private MvService mvService;
	@Autowired
	private MvTypeService MvTypeService;
	@Autowired
	private SingerService singerService;
	
	@RequestMapping("/mv_show")
	public String mv_show(Model model,Integer curPage,MvQuery mvQuery) {
		//如果没有页数，则页数默认为1
		if(curPage==null){
			curPage = 1;
		}
		
		PagerData mvList = mvService.queryByPage(mvQuery, curPage, 5);
		model.addAttribute("mvList", mvList);
		
		List<Map<String, Object>> mvTypeList = MvTypeService.queryMvType();
		model.addAttribute("mvTypeList", mvTypeList);
		
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "admin/mv";
	}
	
	@RequestMapping("/mv_add_show")
	public String mv_add_show(Model model) {
		List<Map<String, Object>> mvTypeList = MvTypeService.queryMvType();
		model.addAttribute("mvTypeList", mvTypeList);
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "admin/mv-add";
	}
	
	@RequestMapping("/mv_add_submit")
	public String mv_add_submit(@RequestParam(required=false) MultipartFile[] wj,HttpServletRequest request) {
				Mv mv = new Mv();
				//获取保存文件的服务器目录
				String saveFilePath = request.getServletContext().getRealPath("myfile/mv/");
				//遍历每个上传文件进行保存
				for(MultipartFile mf : wj){
					//文件名
					String oldFileName = mf.getOriginalFilename();
					//扩展名
					String ext = FilenameUtils.getExtension(oldFileName);
					System.out.println(oldFileName+" "+ext);
					//新的文件名
					String newFileName = UUID.randomUUID()+"_"+oldFileName;
					//构建要保存的文件对象
					File f=new File(saveFilePath,newFileName);
					if(!f.exists()){
						f.mkdirs();
					}
					//将上传文件存到指定的文件对象上
					try {
						mf.transferTo(f);
						if("png".equals(ext)||"jpg".equals(ext)){
							mv.setMvPhoto("myfile/mv/"+newFileName);
						}else if("mp4".equals(ext)){
							mv.setMvMp4("myfile/mv/"+newFileName);
						}
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				String mvName = request.getParameter("mvName");
				String mvTypeId = request.getParameter("mvTypeId");
				String singerId = request.getParameter("singerId");
				mv.setMvName(mvName);
				mv.setMvTypeId(Integer.parseInt(mvTypeId));
				mv.setSingerId(Integer.parseInt(singerId));
		mvService.addMv(mv);
		return "admin/mv-add";
	}
	
	@RequestMapping("/mv_update_show")
	public String mv_update_show(Integer mvId,Model model) {
		Map<String, Object> mvMap = mvService.queryMusicId(mvId);
		model.addAttribute("mvMap", mvMap);
		List<Map<String, Object>> mvTypeList = MvTypeService.queryMvType();
		model.addAttribute("mvTypeList", mvTypeList);
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "admin/mv-edit";
	}
	
	@RequestMapping("/mv_update_submit")
	public String mv_update_submit(Mv mv) {
		mvService.editMv(mv);
		return "admin/mv-edit";
	}
	
	@RequestMapping("/mv_delete")
	public String mv_delete(Integer mvId) {
		mvService.delMv(mvId);
		return "admin/mv";
	}
}
